<template>
	<view class="content">
		<view class="title">投诉内容</view>
		<textarea placeholder="请输入投诉内容..." class="textarea" placeholder-class="pClass" v-model="content"></textarea>
		<!-- <view class="title">投诉图片</view>
		<view class="voice tn-flex">
			<block v-for="(item,index) in FormData.images" :key="index">
				<view class="uimg">
					<image :src="item" class="selectedImg" mode=""></image>
					<image src="/static/my/imgClose.png" class="imgClose" mode=""></image>
				</view>
			</block>
			<image src="https://web.lanmei.co/image/uploading.png" v-if="FormData.images.length < 9" @click="uploadImg" class="uimg"
				mode=""></image>
		</view> -->
		<view class="btn" @click="submit">提交投诉</view>

		<permissionGuide ref="refPermissionGuide" />
	</view>
</template>

<script>
	import {
		imgUploading
	} from "@/common/common.js"
	import permissionGuide from "@/components/permissionGuide/index.vue"
	export default {
		components: {
			permissionGuide
		},
		data() {
			return {
				FormData: {
					images: []
				},
				id: '',
				content: '',
				type: 0
			};
		},
		onLoad(option) {
			this.id = option.id
			this.type = option.type
		},
		methods: {
			async submit() {
				try {
					if (this.content == '') {
						this.msg('请输入投诉内容')
						return
					}
					let result = await this.$request({
						loading: 1,
						method: 'post',
						url: '/api/user/addReport',
						data: {
							goods_id: this.id,
							type: this.type,
							content: this.content
						}
					})
					this.msg(result.data.msg)
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 800)
				} catch (e) {
					this.msg(e.data.msg)
				}
			},
			uploadImg() {
				uni.showActionSheet({
					itemList: ["相册照片", "拍照"],
					success: (res) => {
						if (res.tapIndex == 0) {
							this.handleChooseImage("album");
						}
						if (res.tapIndex == 1) {
							this.handleChooseImage("camera");
						}
					}
				})
			},

			handleChooseImage(sourceType) {
				this.$uniApiBaseChooseImage({
					count: 9 - this.FormData.images.length,
					sourceType: [sourceType],
				}).then((res) => {
					this.recursionUImg(res.tempFilePaths, 0)
				}).catch(error => {
					if (error.errMsg == "show-permision-guide") {
						this.$refs.refPermissionGuide.open({
							perName: sourceType
						})
					}
				})
			},

			// 递归上传图片
			recursionUImg(tempFilePaths, index) {
				uni.showLoading({
					title: '上传中'
				});
				if (index == tempFilePaths.length || this.FormData.images.length == 9) {
					console.log("所有图片上传完成");
					uni.hideLoading();
					return;
				}
				imgUploading(tempFilePaths[index]).then(res => {
					console.log("第" + (index + 1) + "张图片上传成功");
					this.FormData.images.push(res.data.file)
					this.recursionUImg(tempFilePaths, index + 1);
				}).catch(err => {
					console.log("第" + (index + 1) + "张图片上传失败");
					uni.showModal({
						title: '提示',
						content: '上传失败了，可能图片太大导致，请重试'
					});
				})

			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 30rpx;

		.title {
			font-size: 32rpx;
			font-weight: 400;
			color: #333333;
		}

		.pClass {
			font-size: 28rpx;
			font-weight: 400;
			color: #CCCCCC;
		}

		.textarea {
			width: 100%;
			height: 376rpx;
			background: #ffffff;
			border-radius: 16rpx;
			color: #333333;
			padding: 24rpx;
			margin-top: 24rpx;
			margin-bottom: 30rpx;
		}

		.voice {
			margin-top: 24rpx;
			margin-bottom: 30rpx;
			flex-wrap: wrap;

			.uimg:nth-child(4n) {
				margin-right: 0 !important;
			}


			.uimg {
				width: 160rpx;
				height: 160rpx;
				position: relative;
				margin-right: 17rpx;
				margin-bottom: 17rpx;

				.selectedImg {
					width: 100%;
					height: 100%;
				}

				.imgClose {
					width: 49rpx;
					height: 46rpx;
					position: absolute;
					top: 0;
					right: 0;
				}
			}



		}

		.btn {
			height: 88rpx;
			background: linear-gradient(180deg, #DB89F5 0%, #834BF4 100%);
			border-radius: 20rpx;
			font-size: 32rpx;
			font-weight: 500;
			color: #FFFFFF;
			text-align: center;
			line-height: 88rpx;
		}
	}
</style>